package com.qingzhuge.manager.security.service;

import com.qingzhuge.manager.entity.SysUser;
import com.qingzhuge.manager.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zeroxiao
 * @date 2019/9/11 09:17
 */
@Service
@CacheConfig(cacheNames = "sysUserService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ISysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username){
        SysUser user = userService.findByUsernameOrPhone(username);
        if (user == null) {
            throw new UsernameNotFoundException("账号不存在");
        } else {
            return userService.createUserDetails(user);
        }
    }
}
